package com.dwei.sql;

public enum JavaType {
//============================================================================
//enum
//============================================================================
	STRING("CHAR,VARCHAR,LONGVARCHAR,OTHER,CLOB","String","VARCHAR"),
	BIGDECIMAL("NUMERIC,DECIMAL","java.math.BigDecimal","DECIMAL"),
	BOOLEAN("BIT","Boolean","BOOLEAN"),
	INTEGER("TINYINT,SMALLINT,INTEGER,INT","Integer","INTEGER"),
	LONG("BIGINT","Long","BIGINT","L"),
	FLOAT("REAL","Float","REAL","F"),
	DOUBLE("FLOAT,DOUBLE","Double","DOUBLE","D"),
	BYTES("BINARY,VARBINARY,LONGVARBINARY,BLOB","Byte[]","BLOB"),
	DATE("DATE,TIME,DATETIME,TIMESTAMP","java.util.Date","DATE");
//============================================================================
//static function
//============================================================================
	public static JavaType getTypeByJdbc(String jdbc)  {
		jdbc = jdbc.toUpperCase();
		for (JavaType type : JavaType.values()) {
			if (equalJdbc(type.getSqlType(), jdbc))
				return type;
		}
		return null;
	}
	public static JavaType getTypeByJava(String java)  {
		java = java.toUpperCase();
		for (JavaType type : JavaType.values()) {
			if (equalJava(type.getJavaType(), java))
				return type;
		}
		return null;
	}
//============================================================================
//private
//============================================================================
	private static Boolean equalJdbc(String jdbc, String target) {
		String[] jdbcs = jdbc.split(",");
		for (int index = 0;index < jdbcs.length;index++) {
			if (jdbcs[index].equals(target)) return true;
		}
		return false;
	}
	private static Boolean equalJava(String java, String target) {
		int indexJava = java.lastIndexOf(".");
		String javaSub = java.substring(indexJava + 1);
		if (javaSub.endsWith("[]")) javaSub = javaSub.substring(0, javaSub.length() - 2);
		int indexTarget = target.lastIndexOf(".");
		String targetSub = target.substring(indexTarget + 1);
		return targetSub.equals(javaSub);
	}
//============================================================================
//parameter
//============================================================================
	private final String jdbcType;
	private final String sqlType;
	private final String javaType;
	private final String suffix;
//============================================================================
//constructor
//============================================================================
	private JavaType(String sqlType, String type, String jdbcType) {
		this.sqlType = sqlType.toUpperCase();
		this.javaType = type;
		this.suffix = "";
		this.jdbcType = jdbcType;
	}
	
	private JavaType(String sqlType, String type, String jdbcType, String suffix) {
		this.sqlType = sqlType.toUpperCase();
		this.javaType = type;
		this.suffix = suffix;
		this.jdbcType = jdbcType;
	}
//============================================================================
//getter/setter
//============================================================================
	public String getJdbcType() {
		return jdbcType;
	}
	public String getSqlType() {
		return sqlType;
	}
	public String getJavaType() {
		return javaType;
	}
	public String getSuffix() {
		return suffix;
	}
}
